iT邦幫忙

DAY 30
3

Android APP 30天開發實戰系列 第 27

Android App 開發日記 - 我的隨身筆記本(10)

  • 分享至 

  • xImage
  •  

Android App 開發專案過程分享
http://ithelp.ithome.com.tw/ironman6/player/xq3da2/dev/1
ToDo 筆記資料 Activity

由於 MyNotebook 的 Activity 新增資料會發出 Broadcast,由目前的 ToDo Activity 負責接收後馬上新增到資料表中,這個動作特別設計 myResetReceiver 類別來處理。

一進入該頁籤就開啟資料表將目前的筆記資料依照重要度欄位順序取出,並顯示在表格中。同時為每一列資料設定按下事件,以觸發一個編輯筆記對話框。

對話框彈出後,提供使用者進行資料編輯的編輯或刪除處哩,當刪除時從資料欄位進行標註,同時發出 Broadcast 給垃圾桶頁籤 Activity 進行接收處理。

ToDo.java

package tw.brad.android.apps.MyNotebook;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Resources;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.RatingBar;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class ToDo extends Activity {
	private TableLayout table;
	private TextView priority, title;
	private MyDBHelper dbHelper;
	private SQLiteDatabase db;
	private Resources res;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
        setContentView(R.layout.todo);     
        
        res = getResources();
        table = (TableLayout)findViewById(R.id.table);
        
        // ???桀????”
		dbHelper = new MyDBHelper(ToDo.this, "brad", null, 1);
		db = dbHelper.getReadableDatabase();
		resetTable();
		
		IntentFilter filter = new IntentFilter("tw.brad.android.apps.MyNotebook");
		myResetReceiver receiver = new myResetReceiver();
		registerReceiver(receiver, filter);
		
	}

	@Override
	public void finish() {
		super.finish();
        db.close();
        dbHelper.close();
	}

	private class myResetReceiver extends BroadcastReceiver {
		@Override
		public void onReceive(Context context, Intent intent) {
			resetTable();
		}		
	}
	
	private void resetTable(){
		table.removeAllViews();
		
		Cursor cursor = db.query("notebook", null, "status='yet'", null, null, null, "priority desc");        
        while (cursor.moveToNext()){
        	addRecord(new Integer(cursor.getInt(2)).toString(),
        			cursor.getString(1),
        			cursor.getString(3),
        			cursor.getInt(0));
        }
        cursor.close();
		
	}
	
	private void addRecord(String p, String t, String c, int id){
		final int dataid = id;
		final String datap = p;
		final String datac = c;
		final String datat = t;
		
        // ?啣?銝€蝑???		TableRow tr = new TableRow(ToDo.this);
		priority = new TextView(ToDo.this);
		title = new TextView(ToDo.this);
		
		priority.setText(p);
		priority.setTextSize(16);
		priority.setPadding(3, 3, 3, 3);
		priority.setWidth(64);
		
		title.setText(t); 
		title.setTextSize(16);
		title.setPadding(3, 3, 3, 3);
		
		tr.addView(priority, 0);
		tr.addView(title, 1);
		
		tr.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// 靽格鞈?
				editRecord(dataid, datap, datac, datat);
			}
			
		});
		
        table.addView(tr);
	}
	
	private void editRecord(int id, String p, String c, String t){
		// 憿舐內靽格閬?
		AlertDialog alert = null;

		LayoutInflater dialog = LayoutInflater.from(this);
		View dview = dialog.inflate(R.layout.edit, null);

		final int did = id;
		final EditText et = (EditText)dview.findViewById(R.id.editTitle);
		final RatingBar rb = (RatingBar)dview.findViewById(R.id.editPriority);
		final EditText cont = (EditText)dview.findViewById(R.id.editCont);
		
		et.setText(t);
		rb.setRating(new Integer(p));
		cont.setText(c);
		
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle(res.getString(R.string.edit_note));
		builder.setView(dview);
		builder.setCancelable(true);
		builder.setPositiveButton(res.getString(R.string.edit_update), 
				new DialogInterface.OnClickListener(){
			@Override
			public void onClick(DialogInterface dialog, int which) {
				String sql = "UPDATE notebook SET " +
							"title = '" + et.getText() + "', " +
							"content = '" + cont.getText() + "', " +
							"priority = " + rb.getRating() + " WHERE " +
							"_id = " + did;
							
		        db.execSQL(sql);
		        resetTable();
			}
			
		});

		builder.setNegativeButton(res.getString(R.string.edit_delete), 
				new DialogInterface.OnClickListener(){
			@Override
			public void onClick(DialogInterface dialog, int which) {
				String sql ="UPDATE notebook SET " +
							"del = 'false', " +
							"status = 'done' WHERE " +
							"_id = " + did;
				db.execSQL(sql);
		        resetTable();

		        Intent intent = new Intent("tw.brad.android.apps.MyNotebook.finish");
		        sendBroadcast(intent);
		        
			}			
		});

		alert = builder.create();
		alert.show();
	}
	
}

上一篇
Android App 開發日記 - 我的隨身筆記本(9)
系列文
Android APP 30天開發實戰27
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 則留言

我要留言

立即登入留言